

from pygame.locals import *

class EventHandler(object):
    """
    Handles the events that take place in the ui, follows the decorator design
    pattern.
    In order to make this a viable handler the '_hendle_event' fucntion must be
    defined.
    
    When constructing a EventHandler, developers should be aware that each event
    handler will call its sub_handler before rnning its own handle operation,
    therefor constructing an event handler should be done from first handler on
    the bottom to last handler on the top.
    """
    
    def __init__(self, sub_handler = None):
        
        self.sub_handler = sub_handler
        self.can_handle = False
    
    
    
    def handle_event(self, event):
        """
        Handles an event using this handler and passes it on to the next
        handler
        """
        
        if self.sub_handler:
            self.sub_handler.handle_event(event)
        if self.can_handle:
            self._handle_event(event)
            
    def set_sub_handler(self, handler):
        if self.sub_handler:
            self.sub_handler.set_sub_handler(handler)
        else:
            self.sub_handler = handler
        
        
class QuitHandler(EventHandler):
    """
    Controls the quit mechanics
    """
    def __init__(self, quiter, sub_handler = None):
        """
        quiter - The object that must be notified if the game is quit.  Must
                 define the 'quit()' funciton
        """
        EventHandler.__init__(self, sub_handler)
        self.can_handle = True
        self.quiter = quiter
        
    def _handle_event(self, event):
        """Handles quit events"""
        if event.type == QUIT:
            self.quiter.quit()
        if event.type == KEYDOWN and event.key == K_ESCAPE:
            self.quiter.quit()
            
class ArrowHandler(EventHandler):
    """
    catches arrow events
    """
    def __init__(self, arrower, sub_handler = None):
        """
        arrower - notified when arow keys are pressed, must define the 'up',
                  'down', 'left', and 'right' functions
        """
        EventHandler.__init__(self, sub_handler)
        self.can_handle = True
        self.arrower = arrower
        
    def _handle_event(self, event):
        if event.type == KEYDOWN:
            if event.key == K_UP:
                self.arrower.up()
            elif event.key == K_DOWN:
                self.arrower.down()
            elif event.key == K_LEFT:
                self.arrower.left()
            elif event.key == K_RIGHT:
                self.arrower.right()

class KeyHandler(EventHandler):
    """
    catches a specific key being pressed.
    """
    def __init__(self, keyer, key, sub_handler = None):
        """
        keyer - notified when the given key is pressed.
        key - The key that this event handler will catch.
        """
        EventHandler.__init__(self, sub_handler)
        self.keyer = keyer
        self.key = key
        self.can_handle = True
        
    def _handle_event(self, event):
        if event.type == KEYDOWN and event.key == self.key:
            self.keyer.key_pressed(self.key)
    
    